package com.paratice.zookeeper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

public class CuratorDemo {
	
	public static void main(String[] args) {
		try {
			CuratorFramework curatorFramework = CuratorFrameworkFactory
					.builder().connectString("192.168.2.200:2181")
					.sessionTimeoutMs(4000)
					.retryPolicy(new ExponentialBackoffRetry(1000, 3))
					.namespace("curator").build();
			
			curatorFramework.start();
			
			curatorFramework.create().creatingParentContainersIfNeeded()
					.withMode(CreateMode.PERSISTENT)
					.forPath("/zk-persis", "1".getBytes());
			
			Stat stat = new Stat();
			curatorFramework.getData().storingStatIn(stat).forPath("/zk-persis");
			curatorFramework.setData()
					.withVersion(stat.getVersion())
					.forPath("/zk-persis", "2".getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
